package com.harveyguo.nihonngo.controller;

import com.harveyguo.nihonngo.enums.EventLevel;
import com.harveyguo.nihonngo.model.dto.EventDto;
import com.harveyguo.nihonngo.service.EventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 赛事Controller
 */
@RestController
@RequestMapping("/api/events")
public class EventController {

    @Autowired
    private EventService eventService;

    /**
     * 创建赛事
     */
    @PostMapping
    public ResponseEntity<EventDto> createEvent(@RequestBody EventDto eventDto) {
        return ResponseEntity.ok(eventService.createEvent(eventDto));
    }

    /**
     * 根据ID查找赛事
     */
    @GetMapping("/{id}")
    public ResponseEntity<EventDto> getEvent(@PathVariable String id) {
        return ResponseEntity.ok(eventService.findById(id));
    }

    /**
     * 查找所有赛事
     */
    @GetMapping
    public ResponseEntity<List<EventDto>> getAllEvents() {
        return ResponseEntity.ok(eventService.findAll());
    }

    /**
     * 根据拳馆ID查找赛事
     */
    @GetMapping("/gym/{gymId}")
    public ResponseEntity<List<EventDto>> getEventsByGym(@PathVariable String gymId) {
        return ResponseEntity.ok(eventService.findByGymId(gymId));
    }

    /**
     * 根据赛事级别查找
     */
    @GetMapping("/level/{level}")
    public ResponseEntity<List<EventDto>> getEventsByLevel(@PathVariable EventLevel level) {
        return ResponseEntity.ok(eventService.findByLevel(level));
    }

    /**
     * 根据活跃状态查找赛事
     */
    @GetMapping("/active/{active}")
    public ResponseEntity<List<EventDto>> getEventsByActive(@PathVariable Boolean active) {
        return ResponseEntity.ok(eventService.findByActive(active));
    }

    /**
     * 根据状态查找赛事
     */
    @GetMapping("/status/{status}")
    public ResponseEntity<List<EventDto>> getEventsByStatus(@PathVariable String status) {
        return ResponseEntity.ok(eventService.findByStatus(status));
    }

    /**
     * 查找即将开始的赛事
     */
    @GetMapping("/upcoming")
    public ResponseEntity<List<EventDto>> getUpcomingEvents(
            @RequestParam(defaultValue = "#{new java.util.Date()}") Date startDate) {
        return ResponseEntity.ok(eventService.findUpcomingEvents(startDate));
    }

    /**
     * 查找附近的赛事
     */
    @GetMapping("/nearby")
    public ResponseEntity<List<EventDto>> getNearbyEvents(
            @RequestParam Double latitude,
            @RequestParam Double longitude,
            @RequestParam(defaultValue = "10.0") Double radius) {
        return ResponseEntity.ok(eventService.findNearbyEvents(latitude, longitude, radius));
    }

    /**
     * 更新赛事信息
     */
    @PutMapping("/{id}")
    public ResponseEntity<EventDto> updateEvent(@PathVariable String id, @RequestBody EventDto eventDto) {
        return ResponseEntity.ok(eventService.updateEvent(id, eventDto));
    }

    /**
     * 删除赛事
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteEvent(@PathVariable String id) {
        eventService.deleteEvent(id);
        return ResponseEntity.ok().build();
    }

    /**
     * 报名参加赛事
     */
    @PostMapping("/{eventId}/join")
    public ResponseEntity<EventDto> joinEvent(@PathVariable String eventId, @RequestParam String userId) {
        return ResponseEntity.ok(eventService.joinEvent(eventId, userId));
    }

    /**
     * 取消报名
     */
    @PostMapping("/{eventId}/cancel")
    public ResponseEntity<EventDto> cancelEvent(@PathVariable String eventId, @RequestParam String userId) {
        return ResponseEntity.ok(eventService.cancelEvent(eventId, userId));
    }
}